<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="汇编,">





  <link rel="alternate" href="/atom.xml" title="恋恋风辰的个人博客" type="application/atom+xml">






<meta name="description" content="这是汇编语言基础最后一篇，以后还会更新更高级的汇编知识，并且这部分知识会应用到逆向编程的环节，这一章介绍汇编基础–机器指令。 一个16比特位的汇编指令：opcode操作码占用3个比特位，可以表示2的3次方为8种操作 寄存器占用2个比特位，可表示2的2次方为4种可用寄存器 地址空间为2的11次方为2048个可能的内存单元地址可用。">
<meta name="keywords" content="汇编">
<meta property="og:type" content="article">
<meta property="og:title" content="汇编基础最后一篇--机器语言指令">
<meta property="og:url" content="http://www.limerence2017.com/2017/08/15/asmend/index.html">
<meta property="og:site_name" content="恋恋风辰的个人博客">
<meta property="og:description" content="这是汇编语言基础最后一篇，以后还会更新更高级的汇编知识，并且这部分知识会应用到逆向编程的环节，这一章介绍汇编基础–机器指令。 一个16比特位的汇编指令：opcode操作码占用3个比特位，可以表示2的3次方为8种操作 寄存器占用2个比特位，可表示2的2次方为4种可用寄存器 地址空间为2的11次方为2048个可能的内存单元地址可用。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/1.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/2.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/3.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/4.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/5.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/3.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/4.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/6.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/7.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/5.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/6.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/7.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/8.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/9.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/10.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/11.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/12.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/8.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/13.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/14.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/15.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/9.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/10.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/16.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/17.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/18.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/19.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/11.jpg">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/15/asmend/12.jpg">
<meta property="og:updated_time" content="2020-06-22T09:30:58.721Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="汇编基础最后一篇--机器语言指令">
<meta name="twitter:description" content="这是汇编语言基础最后一篇，以后还会更新更高级的汇编知识，并且这部分知识会应用到逆向编程的环节，这一章介绍汇编基础–机器指令。 一个16比特位的汇编指令：opcode操作码占用3个比特位，可以表示2的3次方为8种操作 寄存器占用2个比特位，可表示2的2次方为4种可用寄存器 地址空间为2的11次方为2048个可能的内存单元地址可用。">
<meta name="twitter:image" content="http://www.limerence2017.com/2017/08/15/asmend/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.limerence2017.com/2017/08/15/asmend/">





  <title>汇编基础最后一篇--机器语言指令 | 恋恋风辰的个人博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">恋恋风辰的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Tags
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            Search
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.limerence2017.com/2017/08/15/asmend/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="恋恋风辰">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="恋恋风辰的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">汇编基础最后一篇--机器语言指令</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-08-15T17:14:16+08:00">
                2017-08-15
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/binary/" itemprop="url" rel="index">
                    <span itemprop="name">汇编</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>这是汇编语言基础最后一篇，以后还会更新更高级的汇编知识，并且这部分知识会应用到逆向编程的环节，这一章介绍汇编基础–机器指令。</p>
<p><code>一个16比特位的汇编指令</code>：<br><img src="1.png" alt="1.png"><br>opcode操作码占用3个比特位，可以表示2的3次方为8种操作</p>
<p>寄存器占用2个比特位，可表示2的2次方为4种可用寄存器</p>
<p>地址空间为2的11次方为2048个可能的内存单元地址可用。<br><a id="more"></a><br><code>inc 指令和dec指令</code><br><img src="2.png" alt="2.png"></p>
<p><img src="3.jpg" alt="3.jpg"></p>
<p>从图中可以看出这些指令长度仅为一字节，因为每个指令地址相差为1字节。</p>
<p><code>inc eax</code> 这条指令的机器码为40,  40为16进制表示，转为二进制为 <code>0100 0000</code></p>
<p><code>inc ecx</code>这条指令机器码为41,41位16进制表示，转换为二进制为<code>0100 0001</code></p>
<p>依次展开会发现机器码的规律，<code>开头都为01000XXX</code>,<code>而XXX实际就是寄存器在机器中表示的二进制机器码</code>。</p>
<p>下面是各个寄存器的机器码：<br><img src="4.jpg" alt="4.jpg"><br><img src="5.jpg" alt="5.jpg"></p>
<p>下面看下dec 寄存器指令图：<br><img src="3.png" alt="3.png"><br><img src="4.png" alt="4.png"><br>dec 寄存器指令的格式可以总结为</p>
<p>01001xxx， xxx为寄存器二进制指令格式。</p>
<p>mov 指令<br>mov eax, reg指令图<br><img src="6.jpg" alt="6.jpg"><br>每个指令长度为2字节，从地址偏移可以看出。</p>
<p>将上面指令机器码转为二进制<br><img src="7.jpg" alt="7.jpg"><br><code>mov ecx, eax  十六进制为8B C8，二进制表示第二个字节 1100 1000</code></p>
<p><code>mov ecx, ecx  十六进制为 8B C9，二进制表示第二个字节为 1100 1001</code></p>
<p>通过对比二进制，发现第一个字节都为8B，第二个字节分别为 C0,C1,C2,C3…C9</p>
<p><code>mov reg, reg 指令格式为</code></p>
<p><code>10001011  11XXXYYY</code></p>
<p><code>XXX为目的寄存器， YYY为源寄存器</code></p>
<p> mov reg, imm 即将一个立即数移动到寄存器中指令的机器码会是什么样呢？</p>
<p>下图为将立即数移动到寄存器的图示：<br><img src="5.png" alt="5.png"><br>mov eax, 1 指令机器码为 B8 00000001</p>
<p>mov ecx, 10 指令机器码为 B9 0000000A </p>
<p>两条指令地址相差5个字节，每个字节8bit，可计算出每条指令为40bit长度。</p>
<p>即10个十六进制数表示。而 B8 00000001 和 B9 0000000A 恰巧为10个16进制数字组成。</p>
<p>机器码B8 00000001 从左向右数，去掉B8占用的一个字节，剩下的四个字节可以看出用来表示</p>
<p>立即数 1。同样的道理，可以看出 B9 0000000A也是这个原理。</p>
<p>如果移动的为负数，怎么表示呢？<br><img src="6.png" alt="6.png"><br>mov edx, -1 这个指令我们分析一下 -1在机器中的表现形式</p>
<p>负数在机器中以补码的形式表现，-1 的补码计算规则为：</p>
<p>1的源码为 0000 0000 0000 0000 0000  0000 0000 0001</p>
<p>按位取反 为 1111 1111 1111 1111 1111 1111 1111 1110</p>
<p>末尾+1 位 1111 1111 1111 1111 1111 1111 1111 1111</p>
<p>转换为十六进制为 F F F F F F F F 恰好就是 机器码的最后三个字节表示。</p>
<p>同样的道理适用于mov ebx, -10</p>
<p>下面分析前两个字节 BA, BB, B8， B9 分别有什么关联。<br><img src="7.png" alt="7.png"><br><img src="8.png" alt="8.png"><br>同步对比可以看出 前几个比特位是一样的，都为 10111 </p>
<p>后三个比特位分别为 000，  001， 010， 011，这四个二进制码恰好为</p>
<p>几个寄存器的二进制表示方法。所以</p>
<p>mov reg, imm 机器指令为</p>
<p><code>10111XXX  YYYYYYYY  YYYYYYYY YYYYYYYY YYYYYYYY</code></p>
<p>mov reg, mem　将内存数据移动到寄存器中　的机器指令怎么表示？</p>
<p>mov mem, reg   将寄存器中的数据移动到内存中，机器指令如何表示？<br>下图定义了变量num1, num2, num3<br><img src="9.png" alt="9.png"><br>num1 地址为 0000 0000</p>
<p>num2 地址为 0000 0004</p>
<p>num3 地址为 0000 0008</p>
<p>下图为指令对应的机器码：<br><img src="10.png" alt="10.png"></p>
<p>除去A1，A3开始的一个字节，剩下的4个字节分别为 十六进制4 和十六进制8，分别为num2的地址</p>
<p>和num3 的地址。</p>
<p>下面分析第一个字节A1和A3 规律：<br><img src="11.png" alt="11.png"><br>可以得出结论，无论将内存数据移动到eax中，还是将eax中的数据移动到内存中，</p>
<p>最后的4个字节表示的都是内存的地址，第一个字节表示的不同，用来表示两种移动方式的区别。</p>
<p>总结规律如下：<br><img src="12.png" alt="12.png"><br><code>mov eax, mem 10100001 YYYYYYYY  YYYYYYYY  YYYYYYYY  YYYYYYYY</code></p>
<p><code>mov mem , eax 10100011 YYYYYYYY  YYYYYYYY  YYYYYYYY  YYYYYYYY</code><br>下面图表表示了ebx,ecx,edx三种寄存器和内存数据移动指令<br><img src="8.jpg" alt="8.jpg"></p>
<p>可以看出其他寄存器(eax, ecx, edx)和内存之间移动数据的操作指令大小为6字节，多出的为第二字节，</p>
<p>0D,15，1D。</p>
<p>第一字节8B表示从内存移动数据到寄存器，89表示从寄存器移动到内存，如下图所示：<br><img src="13.png" alt="13.png"><br>第二个字节图表如下：<br><img src="14.png" alt="14.png"><br>通过二进制可以看出 前两位都为 00， 中间三位为 001 ，010， 011 分别表示ecx, edx, ebx</p>
<p>最后三位为101， 其实这个字节不仅仅用于表示移动，还可以表示很多操作，因为ecx为循环控制，</p>
<p>ebx为基址寄存器， edx可用于存余数等等，所以 前两位为00，且最后三位为101，这个组合表示移动</p>
<p>操作。中间三位表示操作的寄存器是什么。</p>
<p>该字节概括为如下图所示：<br><img src="15.png" alt="15.png"><br>mod 字段为00， 且r/m字段值为101，它表示地址模式数据置换，也就是指向内存地址模式<br><code>add 指令 sub 指令</code></p>
<p>add指令很简单，给出图表读者自己分析。<br><img src="9.jpg" alt="9.jpg"></p>
<p>字节转为二进制可以看出从右往左数第3到1位为第二个操作的寄存器，</p>
<p>从右往左数第6到4位表示第一个操作的寄存器， 两个寄存器操作模式为 第7~16位所表示。</p>
<p><code>movoffset 指令  lea 指令</code><br>num2 的值为5， 地址为 00000004， 分别将num2 的内容移动到esi和edi，再通过lea指令将num2地址放入esi和edi<br><img src="10.jpg" alt="10.jpg"><br>通过对比可以看出后四个字节都为 0000 0004 ， 但是前两个字节是不一样的。将前两个字节展开为二进制<br><img src="16.png" alt="16.png"><br>由于mov esi, num2 是将num2数据存入esi，而 lea esi, num2 是将num2地址放入esi，所以</p>
<p>第一个字节的倒数第二位不同，第一个字节分别为10001011  ， 10001101，mov和lea第二个字节是相同的。</p>
<p>下面对比两个lea指令前两个字节  ，第一个字节是相同的，第二个字节为 00 110 101  和 00 111 101</p>
<p>第二个字节中间三位不同，分别为110(esi), 111(edi)表示寄存器。</p>
<p>所以可以总结一下， mov 指令和 lea指令区别在于第一个字节，计算机用第一个字节区别mov和lea指令。</p>
<p>计算机用第二个字节中间三位区别lea指令操作的不同寄存器。</p>
<p>第二个字节和我们上面说过的：<br><img src="17.png" alt="17.png"><br>mod 字段为00， 且r/m字段值为101，它表示地址模式数据置换，也就是指向内存地址模式。<br>下面看一下 mov esi, offset mem 和 mov edi, offset mem两条指令。<br><img src="18.png" alt="18.png"><br>可以看出mov offset指令为5个字节，比 mov 和lea指令少了一个字节，因为mov offset仅仅在编译的时候加载地址，</p>
<p>所以不需要lea的第二个字节表示 数据移动操作。 mov offset是静态的。</p>
<p>mov offset 指令esi和 edi区别仅仅在第一个字节，展开后可以看到：<br><img src="19.png" alt="19.png"><br>第一个字节的后三位 分别为 110(esi), 111(edi)。</p>
<p>可以得出结论mov offset 的指令第一个字节后三位区别esi还是edi，其余不变。<br><code>jmp指令</code></p>
<p>看一则jmp指令操作<br><img src="11.jpg" alt="11.jpg"><br>jmp 指令 机器码为EB + 相对偏移地址 ，如 jmp  around 为EB 04 ，通知计算机跳转到当前指令指针位置+4字节的位置，需要普及一个知识，当程序运行的时候，指令指针或者CPU中的指令指针指向下一条将要取到CPU中被后续执行的指令。当运行到 jump around时，指令指针实际指向了 地址000000D8，指令 above：nop的位置，EB 04指向 为 000 000 DB 加上4个字节地址即为 000 000 DC， 恰好是 around：nop指令地址。</p>
<p>符合逻辑。</p>
<p>下面看下 jump above指令会跳转到哪里。</p>
<p>EB FC 指令 FC 为 1111 1100 ， 该数值为某个负数的补码，负数补码的计算规则为符号位不变，其他位按位取反末位+1。</p>
<p>同样的道理，负数补码转为原码，符号位不变，按位取反末位+1</p>
<p>1111 1100 符号位不变，按位取反 1000 0011， 末位+1，</p>
<p>变为 1000 0100 表示-4.</p>
<p>jmp above 机器指令 EB FC 跳转到 指令指针地址向前移动四个字节的位置。</p>
<p>jmp above 指令运行时，指令指针指向下一条将要取出的指令位置，即 000 000 DC，</p>
<p>000 000 DC - 4 为 000 000 08，即 above：nop 的位置。</p>
<p>到此为止机器指令的知识介绍完毕，以后会介绍高级汇编和反汇编的知识，汇编基础介绍告一段落。</p>
<p>我的微信公众号，谢谢关注。</p>
<p><img src="12.jpg" alt="12.jpg"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/汇编/" rel="tag"># 汇编</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/15/asm6/" rel="next" title="汇编语言学习笔记(六)">
                <i class="fa fa-chevron-left"></i> 汇编语言学习笔记(六)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/16/python1/" rel="prev" title="python自学笔记(一)">
                python自学笔记(一) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">恋恋风辰</p>
              <p class="site-description motion-element" itemprop="description">重剑无锋，大巧不工</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">146</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/secondtonone1" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.zhihu.com/people/wang-wu-chen-32/activities" target="_blank" title="ZhiHu">
                    
                      <i class="fa fa-fw fa-ZhiHu"></i>ZhiHu</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">恋恋风辰</span>

  
  
 
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>
    <span id="busuanzi_container_site_uv">本站访客数<span id="busuanzi_value_site_uv"></span>人</span>
 
 
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
